//
//  YDInquiryResultReferenceView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/3/2.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDInquiryResultReferenceView.h"
#import "YDExpectedReferenceView.h"
#import "YDExpectedReferenceModel.h"


@interface YDInquiryResultReferenceView()

//预期参考
@property (nonatomic , weak) YDExpectedReferenceView *referenceView;

//协议label
@property (nonatomic , weak) UILabel *agreementLabel;

//立即申购按钮
@property (nonatomic , weak) SYNoHeightedButton *referenceButton;

//盈亏比例数组
@property (nonatomic , strong) NSArray *profitRateArray;
//询价参考数组
@property (nonatomic , strong) NSMutableArray *modelArray;


@end

@implementation YDInquiryResultReferenceView
-(NSArray *)profitRateArray
{
    if (_profitRateArray == nil) {
        _profitRateArray = @[@"0" , @"0.2" , @"0.5" , @"1"];
    }
    return _profitRateArray;
}
-(NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

-(void)setReferenceRate:(NSString *)referenceRate
{
    _referenceRate  = referenceRate;
    //刷新询价参考数据
    
    
}
-(void)setNominalPrincipal:(NSString *)nominalPrincipal
{
    _nominalPrincipal = nominalPrincipal;
    
    [self resloveDataWithReferenceViewWithReferenceRate:self.referenceRate NominalPrincipal:self.nominalPrincipal];
    
    
    self.referenceView.modelArray = self.modelArray;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self referenceView];
        
        [self agreementLabel];
        
        [self referenceButton];
    }
    return self;
}

+(instancetype)initInquiryResultReferenceViewWithReferenceRate:(NSString *)referenceRate NominalPrincipal:(NSString *)nominalPrincipal
{
    YDInquiryResultReferenceView *referenceView = [[self alloc] init];


    
    referenceView.referenceRate = referenceRate;
    referenceView.nominalPrincipal = nominalPrincipal;
    
    return referenceView;
}

-(YDExpectedReferenceView *)referenceView
{
    if (_referenceView == nil) {
        YDExpectedReferenceView *referenceView = [YDExpectedReferenceView initExpectedReferenceViewWithReferenceDataArray:self.modelArray];
        [self addSubview:referenceView];
        referenceView.frame = CGRectMake(rate375(20), isPhoneX ? rateX812(10) : rate750(10), (LKScreenW - rate375(20) * 2), isPhoneX ? rateX812(135) : rate750(135));
        _referenceView = referenceView;
    }
    return _referenceView;
}


-(UILabel *)agreementLabel
{
    if (_agreementLabel == nil) {
        UILabel *agreementLabel = [[UILabel alloc] init];
        [self addSubview:agreementLabel];
        agreementLabel.numberOfLines = 0;
        agreementLabel.font = [UIFont systemFontOfSize:font10ScreenWRate * LKScreenW];
        agreementLabel.textColor = [UIColor colorWithHexString:dark];
        NSString *label_text = @"点击‘立即认购’代表您已充分了解并同意《沪深A股交易融资合作协议》和《风控管理服务协议》";
        NSMutableAttributedString *label_attributedString = [[NSMutableAttributedString alloc] initWithString:label_text];
        
        [label_attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font10ScreenWRate * LKScreenW] range:NSMakeRange(0, label_text.length)];
        
        [label_attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:frenchBlue] range:NSMakeRange(19, 14)];
        [label_attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:frenchBlue] range:NSMakeRange(34, 10)];
        
        
        agreementLabel.attributedText = label_attributedString;
        
        [agreementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.referenceView);
            make.top.equalTo(self.referenceView.mas_bottom).offset(isPhoneX ? rateX812(10) : rate750(10));
            make.height.equalTo(@(isPhoneX ? rateX812(30) : rate750(30)));
        }];
        
        LK(weakSelf)
        [agreementLabel yb_addAttributeTapActionWithStrings:@[@"《风控管理服务协议》",@"《沪深A股交易融资合作协议》"] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
            if ([string isEqualToString:@"《风控管理服务协议》"]) {
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(inquiryResultReferenceView:ClickStyle:)]) {
                    [weakSelf.delegate inquiryResultReferenceView:weakSelf ClickStyle:ReferenceViewXuZhi];
                }
            }else if ([string isEqualToString:@"《沪深A股交易融资合作协议》"]){
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(inquiryResultReferenceView:ClickStyle:)]) {
                    [weakSelf.delegate inquiryResultReferenceView:weakSelf ClickStyle:ReferenceViewHeZuo];
                }
            }
        }];
        //是否设置点击效果
        agreementLabel.enabledTapEffect = YES;
        
        
        _agreementLabel = agreementLabel;
    }
    return _agreementLabel;
}

-(SYNoHeightedButton *)referenceButton
{
    if (_referenceButton == nil) {
        SYNoHeightedButton *referenceButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:referenceButton];
        
        referenceButton.backgroundColor = [UIColor colorWithHexString:Orange];
        [referenceButton setTitle:@"立即认购" forState:UIControlStateNormal];
        [referenceButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        referenceButton.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        referenceButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        referenceButton.titleLabel.adjustsFontSizeToFitWidth = YES;
        [referenceButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.equalTo(@(rate375(284)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(42)));
            }else{
                make.height.equalTo(@(rate750(42)));
            }
            make.top.equalTo(self.referenceView.mas_bottom).offset(isPhoneX ? rateX812(50) : rate750(50));
        }];
        referenceButton.layer.cornerRadius = 3.0;
        referenceButton.layer.masksToBounds = YES;
        [referenceButton addTarget:self action:@selector(referenceButtonClick) forControlEvents:UIControlEventTouchDown];
        
        _referenceButton = referenceButton;
    }
    return _referenceButton;
}
-(void)referenceButtonClick
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(inquiryResultReferenceView:ClickStyle:)]) {
        [self.delegate inquiryResultReferenceView:self ClickStyle:ReferenceViewRenGou];
    }
}

//处理数据
-(void)resloveDataWithReferenceViewWithReferenceRate:(NSString *)referenceRate NominalPrincipal:(NSString *)nominalPrincipal
{
    if (([referenceRate floatValue] <= 0 || !referenceRate.length) || ([nominalPrincipal floatValue] <= 0 || !nominalPrincipal.length))return;
    
    for (int i = 0; i < 5; i ++) {
        
        YDExpectedReferenceModel *model = [YDExpectedReferenceModel new];
        if (i == 0) {
            model.hopeRate = @"预期涨幅";
            model.profitRate = @"盈亏比例";
            model.profitMoney = @"盈利金额";
        }else{
            //权利本金
            NSString *rightMoney = [self getRightPrincipalWithRate:referenceRate andNominalPrincipal:nominalPrincipal];
            NSString *profitRate = self.profitRateArray[i - 1];//盈亏比例
            //盈利金额
            NSString *profitMoney = [[profitRate pz_stringNumberByMultiplyingBy:rightMoney] pz_stringNumberByAdding:rightMoney];
            
            //预期涨幅
            NSString *hopeRate = [self getHopeRateWithProfitMoney:profitMoney andNominalPrincipal:nominalPrincipal];
            
            model.hopeRate = hopeRate;
            model.profitRate = [self getPersentProfitRateWithProfitRate:profitRate];
            model.profitMoney = profitMoney;

        }
        [self.modelArray addObject:model];
    }

    
}

//获取权利本金 = 询价比例 * 名义本金
-(NSString *)getRightPrincipalWithRate:(NSString *)rate andNominalPrincipal:(NSString *)nominalPrincipal
{
    if (([rate floatValue] <= 0 || !rate.length) || ([nominalPrincipal floatValue] <= 0 || !nominalPrincipal.length)) {
        return @"---";
    }else{
        return [rate pz_stringNumberByMultiplyingBy:nominalPrincipal];
    }
}


//计算盈利金额
// 盈利金额 = 盈亏比例 * 权利本金 + 权利本金
-(NSString *)getProfitMoneyWithProfitRate:(NSString *)profitRate andRightPrincipal:(NSString *)rightPrincipal
{
    
    if ([rightPrincipal isEqualToString:@"---"]) {
        return @"---";
    }else{
        return [[profitRate pz_stringNumberByMultiplyingBy:rightPrincipal] pz_stringNumberByAdding:rightPrincipal];
    }
}

//计算预期涨幅  保留两位小数
//预期涨幅 = 盈利金额 / 名义本金
-(NSString *)getHopeRateWithProfitMoney:(NSString *)profitMoney andNominalPrincipal:(NSString *)nominalPrincipal
{
    if ([profitMoney isEqualToString:@"---"] || [nominalPrincipal isEqualToString:@"---"]) {
        return @"---";
    }else{

        NSString *string =  [[profitMoney pz_stringNumberByMultiplyingBy:@"100"] pz_stringNumberByDividingBy:nominalPrincipal];
        
        return [NSString stringWithFormat:@"%@%@" , string , @"%"];
    }
}

//获取模型里的盈亏比例 ， 带百分号
-(NSString *)getPersentProfitRateWithProfitRate:(NSString *)profitRate
{
    
//    PZDecimalNumberHander *hander = [PZDecimalNumberHander shareHanderWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:YES raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
//    NSString *string = [profitRate pz_stringNumberByMultiplyingBy:@"100" withBehavior:hander];
    
    NSString *string = [profitRate pz_stringNumberByMultiplyingBy:@"100"];
    return [NSString stringWithFormat:@"%@%@" , string , @"%"];
}

@end
